package com.sprouting.ops.service;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sprouting.ops.dao.OpsAuthorityDao;
import com.sprouting.ops.dao.OpsRoleAuthorityDao;
import com.sprouting.ops.dao.OpsRoleDao;
import com.sprouting.ops.interfaces.ConstantPower;
import com.sprouting.ops.model.OpsAuthority;
import com.sprouting.ops.model.OpsRole;
import com.sprouting.ops.model.OpsRoleAuthority;
import com.sprouting.ops.model.ResultJson;
import com.sprouting.ops.model.vo.PowerTree;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Power)表服务实现类
 *
 * @author longx
 * @since 2023-03-19 22:44:34
 */
@Service
@Slf4j
public class OpsAuthorityService extends ServiceImpl<OpsAuthorityDao, OpsAuthority> {

    @Resource
    private OpsAuthorityDao opsAuthorityDao;
    @Resource
    private OpsRoleDao opsRoleDao;
    @Resource
    private OpsRoleAuthorityDao opsRoleAuthorityDao;


    /**
     * 根据角色ID查询权限
     * @param roleIdSet 角色，可以有多个
     * @return
     */
    public List<OpsAuthority> getPowerByRoleId(List<String> roleIdSet) {
        return opsAuthorityDao.getPowerByRoleId(roleIdSet);
    }

    /**
     * 根据权限标识符查询权限
     * @param id 主键，传则忽略
     * @param permission 权限标识符
     * @return
     */
    public OpsAuthority getByPermission(String id, String permission) {
        return opsAuthorityDao.getByPermission(id, permission);
    }

    /**
     * 根据菜单权限查询 权限
     * @param id id 主键，传则忽略
     * @param webCode 菜单权限
     * @return
     */
    public OpsAuthority getByWebCode(String id, String webCode) {
        return opsAuthorityDao.getByWebCode(id, webCode);
    }

    /**
     * 获取当前用户拥有的权限
     * @param userId 要查询的用户
     * @return
     */
    public List<PowerTree> getPowerTreeDtoByUserId(String userId) {
        // 先查询角色，判断是否拥有超级角色
        List<OpsRole> roleList = opsRoleDao.getRoleHaveByUserId(userId);
        if (roleList == null || roleList.size() == 0){
            return null;
        }

        // 超级管理员现在就判断是否角色名是否基础角色
        List<OpsRole> adminList = roleList.stream().filter(obj -> ConstantPower.Base.BASE_ROLE_CODE.equals(obj.getCode())).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(adminList)){
            log.info("[权限] 超级管理员上线");
            List<OpsAuthority> authorityList = opsAuthorityDao.selectList(null);
            if (CollUtil.isEmpty(authorityList)){
                return null;
            }

            return BeanUtil.copyToList(authorityList, PowerTree.class);
        }

        List<String> roleIdList = roleList.stream().map(OpsRole::getId).collect(Collectors.toList());
        List<OpsAuthority> powerList = opsAuthorityDao.getPowerByRoleId(roleIdList);
        if (CollUtil.isEmpty(powerList)){
            return null;
        }

        return BeanUtil.copyToList(powerList, PowerTree.class);
    }

    /**
     * 删除权限
     * @param id 权限id
     */
    @Transactional(rollbackFor = Exception.class)
    public void delPower(String id) {
        opsAuthorityDao.deleteById(id);

        LambdaUpdateWrapper<OpsRoleAuthority> roleAuthWrapper = new LambdaUpdateWrapper<>();
        roleAuthWrapper.eq(OpsRoleAuthority::getAuthorityId, id);
        opsRoleAuthorityDao.delete(roleAuthWrapper);
    }

    /**
     * 根据角色获取权限
     * @param roleIdList 角色ID
     * @return
     */
    public List<OpsAuthority> getByRoleIdBatch(List<String> roleIdList) {
        return opsAuthorityDao.getByRoleIdBatch(roleIdList);
    }
}

